# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# Author     ：skyTree
# version    ：python 3.11
# Description分类视图
"""
from flask import request
from api.models import Attribute, Category
from api.attrs import attrs_api, attrs_blue
from api.utils.response import to_response
from api.utils.baseResource import BaseResource
from api import db
from api.utils.tokens import login_required


class AttrsResource(BaseResource):
    def get(self):
        id = request.args.get('id')
        if not id:
            return to_response(10000)
        try:
            attr = Attribute.query.get(id)
        except Exception as e:
            return to_response(10020)
        if not attr:
            return to_response(10017)

        return to_response(200, data=attr.to_dict(), msg='查询属性成功！')

    def post(self):
        try:
            name = request.json.get('name')
            val = request.json.get('val')
            cid = request.json.get('cid')
            _type = request.json.get('_type')

            if not all([name, cid, _type]):
                return to_response(10000)
            if val:
                attr = Attribute(name=name, val=val, cid=cid, _type=_type)
                db.session.add(attr)
                db.session.commit()
                return to_response(200, data=attr.to_dict(), msg='新增属性成功！')
            else:
                attr = Attribute(name=name, cid=cid, _type=_type)
                db.session.add(attr)
                db.session.commit()
                return to_response(200, data=attr.to_dict(), msg='新增属性成功！')
        except Exception as e:
            return to_response(10020)

    def put(self):
        id = request.json.get('id')
        name = request.json.get('name')
        val = request.json.get('val')
        cid = request.json.get('cid')
        _type = request.json.get('_type')
        print(id, name, cid, _type)
        if not all([id, name, cid, _type]):
            return to_response(10000)
        try:
            attr = Attribute.query.get(id)
        except Exception as e:
            return to_response(10020)

        if not attr:
            return to_response(10017)
        try:
            if val:
                attr.name = name
                attr.val = val
                attr.cid = cid
                attr._type = _type
                db.session.commit()
                return to_response(200, data=attr.to_dict(), msg='修改属性成功！')
            else:
                attr.name = name
                attr.cid = cid
                attr._type = _type
                db.session.commit()
                return to_response(200, data=attr.to_dict(), msg='修改属性成功！')
        except Exception as e:
            return to_response(10020, msg=str(e))

    def delete(self):
        id = request.args.get('id')
        print(id)
        if not id:
            return to_response(10000)
        try:
            attr = Attribute.query.get(id)
        except Exception as e:
            return to_response(10020)
        if not attr:
            return to_response(10017)
        try:
            db.session.delete(attr)
            db.session.commit()
            return to_response(200, data=attr.to_dict(), msg='删除属性成功！')
        except Exception as e:
            return to_response(10020)


attrs_api.add_resource(AttrsResource, '/attrs')


@attrs_blue.route('/attrs_list/')
@login_required
def get_category_attrs():
    cid = request.args.get('cid')
    _type = request.args.get('_type')

    if not all([cid, _type]):
        return to_response(10000)

    try:
        category = Category.query.get(cid)
    except Exception as e:
        return to_response(10020)

    if not category:
        return to_response(10017)
    if _type == 'static':
        data = [attr.to_dict() for attr in category.attrs if attr._type == 'static']
    else:
        data = [attr.to_dict() for attr in category.attrs if attr._type == 'dynamic']
    return to_response(200, data=data, msg='查询属性成功！')
